VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsXML"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Public colXMLDataHolder As Collection
Public colXMLImageHolder As Collection

Public Function ReadXMLFile(ByVal strPath As String, ByRef strReturnedError As String) As Boolean
  Dim root As MSXML.IXMLDOMElement
  Dim child As MSXML.IXMLDOMNode
  Dim images As MSXML.IXMLDOMNodeList
  Dim mobjXMLRead As MSXML.DOMDocument
  Dim mobjXMLWrite As New MSXML.DOMDocument
  
  Set mobjXMLRead = New MSXML.DOMDocument
  Set mobjXMLWrite = New MSXML.DOMDocument
  Set colXMLDataHolder = New Collection
  
  On Error GoTo READXML_FIX
  ' Parse the file synchcronously, you may want to parse it asynchronously
  ' to handle a progress bar as the file is read for example
  mobjXMLRead.async = False
  
  ' Don't bother validating this XML file
  mobjXMLRead.validateOnParse = False
  
  ' Read the XML file
  Call mobjXMLRead.Load(strPath)
  
  ' Set the root node to the XML document root element, "ScanOutput"
  Set root = mobjXMLRead.documentElement
  
  ' Display each node as we walk the tree
'  For Each child In root.childNodes
'    colXMLDataHolder.Add child.nodeName & " : " & child.Text
'  Next
  
  ' Get attributes by name
  colXMLDataHolder.Add "Source:" & root.getAttribute("Source")
  colXMLDataHolder.Add "Scan Station ID: " & root.getAttribute("ID")
  
  ' Display node values for specific keys
  colXMLDataHolder.Add "Date: " & root.childNodes.Item(0).Text
  colXMLDataHolder.Add "Time: " & root.childNodes.Item(1).Text
  colXMLDataHolder.Add "From: " & root.childNodes.Item(2).Text
  colXMLDataHolder.Add "FreeText: " & root.childNodes.Item(4).Text
  colXMLDataHolder.Add "UserID: " & root.childNodes.Item(5).Text
  
  ' Display the image attributes: Path-name and Image-Count
  colXMLDataHolder.Add "Images Path: " & root.childNodes.Item(3).Attributes.Item(0).Text
  colXMLDataHolder.Add "Image Count: " & root.childNodes.Item(3).Attributes.Item(1).Text
  
  ' Walk the images tree to display the image names and path attributes
  Set images = root.childNodes.Item(3).childNodes
  For Each child In images
    colXMLDataHolder.Add child.nodeName & " : " & child.Text
    'adding the images to their own collection for convienience
    colXMLImageHolder.Add child.nodeName & " : " & child.Text
  Next
  ReadXMLFile = True
  Exit Function
READXML_FIX:
  ReadXMLFile = False
  strReturnedError = "XML read failed : " & mobjXMLRead.parseError.reason
End Function

Public Function CreateXMLFile(ByVal strID As String, ByVal strUserID As String, _
                            ByVal strImagePath As String, ByVal strImageList As String, _
                            ByVal strFreeText As String, ByVal strXMLFilePath As String, _
                            ByVal strFileName As String, ByVal strImageCount As String, _
                            ByVal strSource As String, ByRef strReturnedError As String) As Boolean
Dim iFree As Integer
Dim strDate As String
Dim strTime As String
Dim arrImageList() As String
Dim i As Integer

On Error GoTo XML_FIX
'this function will create an XML file with the passed in values
'chop up the list of images into an array

arrImageList = Split(strImageList, ",", -1, vbTextCompare)

'set the time variables
strDate = Format(Now, "DD/MM/YYYY")
strTime = Format(Now, "HH:MM:SS")

'Here we create the XML File in the correct path and give it the correct name
iFree = FreeFile
Open strXMLFilePath & "\" & strFileName For Append As #iFree

Print #iFree, "<?xml version=""1.0"" encoding=""UTF-8""?>"
Print #iFree, "<ImageProcessOutput Source=""" & strSource & """ ID=""" & strID & """>"
Print #iFree, vbTab & "<Date>" & strDate & "</Date>"
Print #iFree, vbTab & "<Time>" & strTime & "</Time>"
Print #iFree, vbTab & "<UserID>" & strUserID & "</UserID>"
Print #iFree, vbTab & "<Images Path=""" & strImagePath & """ Count=""" & strImageCount & """>"
'loop to the number of images
For i = 0 To UBound(arrImageList)
    Print #iFree, vbTab & vbTab & "<Filename>" & arrImageList(i) & "</Filename>"
Next
Print #iFree, vbTab & "</Images>"
Print #iFree, vbTab & "<Freetext>" & strFreeText & "</Freetext>"
Print #iFree, "</ImageProcessOutput>"
Close #iFree
CreateXMLFile = True
Exit Function

XML_FIX:
    CreateXMLFile = False
    strReturnedError = "XML File create failed : " & Err.Description
End Function

Public Property Get StationID() As String
StationID = colXMLDataHolder.Item(2)
End Property

Public Property Get Source() As String
Source = colXMLDataHolder.Item(1)
End Property
Public Property Get theDate() As String
theDate = colXMLDataHolder.Item(3)
End Property
Public Property Get theTime() As String
theTime = colXMLDataHolder.Item(4)
End Property
Public Property Get UserId() As String
UserId = colXMLDataHolder.Item(5)
End Property
Public Property Get FreeText() As String
FreeText = colXMLDataHolder.Item(6)
End Property
Public Property Get ImagePath() As String
ImagePath = colXMLDataHolder.Item(8)
End Property
Public Property Get ImageCount() As String
ImageCount = colXMLDataHolder.Item(9)
End Property

Private Sub Class_initialize()
Set colXMLDataHolder = New Collection
Set colXMLImageHolder = New Collection
End Sub
